import 'dart:core';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:qaun_ni_dev/other/contacts_page.dart';
import 'item/conversation_list_item.dart';
import 'package:provider/provider.dart';
import 'package:qaun_ni_dev/provider/rongImProvider.dart';

// --------------------------------------------------------------------------------------------------------------------
class ConversationListPage extends StatefulWidget {
  @override
  _ConversationListPagesState createState() => _ConversationListPagesState();
}

class _ConversationListPagesState extends State<ConversationListPage> {
  @override
  void initState() {
    super.initState();
    Provider.of<RongImProvider>(context,listen: false).getConversionList();
    _renfreshUI();
  }

  void _renfreshUI() {
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    print('打印输出 会话列表页面是否会刷新呢');
    return Scaffold(
      appBar: AppBar(
        title: Text('会话列表'),
        actions: <Widget>[
          IconButton(
            color: Colors.white,
            icon: Icon(Icons.assignment_ind),
            onPressed: () {
              Navigator.push(context, MaterialPageRoute(
                builder: (context) => ContactsPage()
              ));
            },
          )
        ],
      ),
      body: Consumer<RongImProvider>(
        builder: (context,data,_) {
          if(data.conList.length >0) {
//            return _tempWidget(data.conList);
          return _buildConversationListView(data.conList);
          } else {
            return Center(
              child: Text('加载'),
            );
          }
        },
      ),
//      key: UniqueKey(),
    );
  }

  Widget _buildConversationListView(List conListData) {
    return Container(
      child: ListView.builder(
        key: UniqueKey(),
        itemCount: conListData.length,
        itemBuilder: (context,index) {
             return ConversationListItemWidget(conversation:conListData[index]);
          }
          ),
    );
  }


  Widget _tempWidget(List conListData) {
    return Container(
      child: ListView.builder(
        itemCount: conListData.length,
        itemBuilder: (context,index) {
          return Text('${conListData[index].targetId}');
        }
      ),
    );
  }

  @override
  void deactivate() {
    // TODO: implement deactivate
    super.deactivate();
    var bool = ModalRoute.of(context).isCurrent;
    if(bool) {
      print('333333333333333333333333333333333333333');
      ConversationListPage();
    }
  }


}
